package com.share.barter.action;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Tumi
 *
 */
public class UploadPhotoAction extends ActionSupport implements ServletRequestAware {
	
	//private File uploadFile;

	 private static final long	serialVersionUID	= 1L;
	
	 private File uploadFile;
	 private InputStream is = null;
	 private String uploadFileFileName = null;
	 private OutputStream os;
	
	 private HttpServletRequest request;

	 @Override
	 public String execute() throws Exception {
	  // String fileName = myfile.getName();
	  this.is = new FileInputStream(uploadFile);
	
	  String path = request.getSession().getServletContext().getRealPath(
	    "/upload");
	
	  this.os = new FileOutputStream(path + "/"
	    + this.getUploadFileFileName());
	
	  // byte[] by = new byte[2048];
	  int a = 0;
	  while ((a = this.is.read()) != -1) {

		  os.write(a);
	  }

  return this.SUCCESS;
 }
	
	 public void setServletRequest(HttpServletRequest arg0) {
	  this.request = arg0;
	
	 }
	
	 public String getUploadFileFileName() {
	  return uploadFileFileName;
	 }
	
	 public void setUploadFileFileName(String uploadFileFileName) {
	  this.uploadFileFileName = uploadFileFileName;
	 }
	
	 public File getUploadFile() {
	  return uploadFile;
	 }
	
	 public void setUploadFile(File uploadFile) {
	  this.uploadFile = uploadFile;
	 }

}